home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12203 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  50 lines

  1. Path: news1.erols.com!newsmaster@erols.com
  2. From: "Stephen C. Marney" <scmarney@erols.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: what getline returns? Nobody can explain this!!
  5. Date: Mon, 18 Mar 1996 16:06:49 -0500
  6. Organization: Techsyn Company
  7. Message-ID: <314DD069.3D0B@erols.com>
  8. References: <4ifbk8$6nf@bcarh8ab.bnr.ca>
  9. NNTP-Posting-Host: as6s48.erols.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=iso-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  14.  
  15. getline returns *this but ios (superclass of istream) defines the following overloaded operator:
  16. operator void* () const;
  17.  
  18. as follows: (ios.h)
  19. operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }
  20.  
  21. So we are getting an implicit conversion to an int used for the if statement.
  22.   
  23. MS Visual C++ documentation:
  24. "An operator that converts a stream to a pointer that can be compared to 0.
  25. Return Value
  26. The conversion returns 0 if either failbit or badbit is set in the streamÆs error state.See rdstate for a 
  27. description of the error state masks. A nonzero pointer is not meant to be dereferenced."
  28.  
  29. whyme wrote:
  30. > getline member fuction in ifstream is supposed to return the
  31. > receiving object (that is, ostream&).
  32. > However, many books contain the code samples like:
  33. > ifstream in("tmp.txt");
  34. > while (in.getline(buffer,sizeof(buffer))) { ...  }
  35. > This loop quits only if in.getline returns 0 or NUll;
  36. >                 ^^^^^^^
  37. > but an ostream object is neither 0 nor NULL;
  38. > the result is an object, not integer or pointer.
  39. > So how can this loop quit?
  40. > It works; but no one I know can explain this!!!!
  41. > Thanks for your help.
  42.  
  43.